/*
 * ColumnTree 组件
 */
CQCIS.tree.ColumnTree = Ext.extend(Ext.tree.TreePanel, {
    initState: Ext.emptyFn,
    saveState: Ext.emptyFn,
    checkedRows: [],
    lines: false,
    checkbox: true,
    //borderWidth : Ext.isBorderBox ? 0 : 2,
    border: false,
    cls: 'x-column-tree',
    rootVisible: true,
    checkbox: true,
    lines: true,
    autoScroll: true,
    nodeDraggable: false,
    selModel: 'm',
    initComponent: function(){
        if (typeof this.selModel == 'string') {
            this.selModel = this.selModel == 'm' ? new CQCIS.tree.ColumnTreeSelectionModel({}) : new Ext.tree.DefaultSelectionModel({});
        }
        if (!this.root) {
            this.root = new CQCIS.tree.ColumnTreeRootNode({// 设置根节点
                id: this.rootId,
                uiProvider: CQCIS.tree.ColumnRootUI,
                expandable: false,
                expanded: true,
                checked: false,
                text: this.rootText
            });
        }
        if (!this.loader && this.dwrCall) {
            this.loader = new CQCIS.tree.DWRTreeLoader({
                baseAttrs: {
                    uiProvider: 'col'
                },
                dwrCall: this.dwrCall,
                nodeMeta: this.nodeMeta,
                listeners: this.loaderListeners,
                uiProviders: {
                    'col': CQCIS.tree.ColumnNodeUI
                }
            });
        }
        
        this.addEvents({
            /**
             * @event on added
             */
            'added': true
        });
        
        CQCIS.tree.ColumnTree.superclass.initComponent.call(this);
    },
    onRender: function(){
        CQCIS.tree.ColumnTree.superclass.onRender.apply(this, arguments);
        this.headers = this.body.createChild({
            cls: 'x-tree-headers'
        }, this.innerCt.dom);
        var cols = this.columns, c;
        var totalWidth = 0;
        for (var i = 0, len = cols.length; i < len; i++) {
            c = cols[i];
            totalWidth += c.width;
            this.headers.createChild({
                cls: 'x-tree-hd ' + (c.cls ? c.cls + '-hd' : ''),
                cn: {
                    cls: 'x-tree-hd-text',
                    html: c.header
                },
                style: 'width:' + (c.width - 2) + 'px;',
                id: c.id ? c.id : ""
            });
        }
        this.headers.createChild({
            cls: 'x-clear'
        });
        // prevent floats from wrapping when clipped
        this.headers.setWidth(totalWidth);
        this.innerCt.setWidth(totalWidth);
        this.on('contextmenu', function(node){
            node.select();
        }, this);
        
        if (this.el.parent().dom.name && this.el.parent().dom.name == 'mainContent') {
            this.on('render', function(){
                this.syncSize();
            }, this, {
                single: true
            });
            
            this.on('added', function(o, ct){
                if (ct.rendered) {
                    if (ct.initialConfig.layout === 'border') {
                        o.setWidth((ct.body.getWidth() - 10) / ct.initialConfig.items.length);
                    } else {
                        o.setWidth(ct.body.getWidth() - 10);
                    }
                    o.setHeight(ct.body.getHeight() - 10);
                } else {
                    ct.on('render', function(){
                        o.setWidth(ct.body.getWidth() - 10);
                        o.setHeight(ct.body.getHeight() - 10);
                    }, ct, {
                        single: true
                    });
                }
            }, this, {
                single: true
            });
        }
    },
    syncSize: function(){
        if (top.center) {
            this.setSize({
                width: top.center.getSize().width,
                height: top.center.getSize().height - 28
            });
        }
    },
    /**
     * 在TOP工具栏增加组件
     */
    addTop: function(){
        var a = arguments, l = a.length;
        var tbar = this.getTopToolbar();
        for (var i = 0; i < l; i++) {
            tbar.add(a[i]);
        }
    },
    
    /**
     * 在BOTTOM工具栏增加组件
     */
    addBottom: function(){
        var a = arguments, l = a.length;
        var bbar = this.getBottomToolbar();
        for (var i = 0; i < l; i++) {
            bbar.add(a[i]);
        }
    },
    getSelectedRows: function(){
        var values = [];
        var key = this.columns[0].id;
        for (var i = 0; i < this.selModel.selNodes.length; i++) {
            var obj = {};
            if (this.selModel.selNodes[i].isRoot) {
                if (this.selModel.selNodes[i].data) {
                    obj = this.selModel.selNodes[i].data;
                } else {
                    obj[key] = this.selModel.selNodes[i].id;
                }
            } else {
                obj[key] = this.selModel.selNodes[i].attributes[key];
                for (var j = 0; j < this.columns.length; j++) {
                    obj[this.columns[j].dataIndex] = this.selModel.selNodes[i].attributes[this.columns[j].dataIndex];
                }
            }
            values.push(obj);
        }
        return values;
    }
});
